package com.dc.ftp.activity.loan;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.dc.ftp.R;
import com.dc.ftp.base.SPBaseActivity;
import com.dc.ftp.interfaces.OnBooleanListener;
import com.dc.ftp.view.mycamera.CameraSurfaceView;


public class MyTakingPhotoActivity extends SPBaseActivity {
    private CameraSurfaceView mCameraSurfaceView;
    private Button mButton;
    private TextView tvRight;
    private String tag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_taking_photo);
        super.init();
        setHeadTitle(true,"拍照");

    }

    @Override
    public void initView() {
        mCameraSurfaceView = (CameraSurfaceView) findViewById(R.id.cameraSurfaceView);
        mButton = (Button) findViewById(R.id.takePic);
        tvRight = (TextView) findViewById(R.id.tv_share);
        tvRight.setText("重拍");
    }

    @Override
    public void initData() {
        Intent in = getIntent();
         tag = in.getStringExtra("tag");


    }

    @Override
    public void initEvent() {
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mButton.getText().equals("拍照")){

                    onPermissionRequests(Manifest.permission.CAMERA, new OnBooleanListener() {
                        @Override
                        public void onClick(boolean bln) {
                            if (bln) {
                                showToast("权限CAMERA OK");
                                mCameraSurfaceView.takePicture();


                            } else {
                                showToast("摄像头或无法正常使用");
                            }
                        }
                    });




                    //mCameraSurfaceView.takePicture();

                    mButton.setText("完成");
                    tvRight.setVisibility(View.VISIBLE);
                }else if(mButton.getText().equals("完成")){

                      setResult(556,new Intent().putExtra("photoPath",mCameraSurfaceView.filePath).putExtra("tag",tag));
                      finish();
                }


            }
        });

        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCameraSurfaceView.reTakePhoto();
                mButton.setText("拍照");
                tvRight.setVisibility(View.GONE);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        onPermissionRequests(Manifest.permission.READ_EXTERNAL_STORAGE, new OnBooleanListener() {
            @Override
            public void onClick(boolean bln) {
                if (bln) {
                    showToast("权限READ OK");



                } else {
                    showToast("读文件或无法正常使用");
                }
            }
        });

        onPermissionRequests(Manifest.permission.WRITE_EXTERNAL_STORAGE, new OnBooleanListener() {
            @Override
            public void onClick(boolean bln) {
                if (bln) {
                    showToast("权限WRITE OK");



                } else {
                    showToast("写文件或无法正常使用");
                }
            }
        });



    }
}
